<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Ahasmany extends Controller {

    public function before() {
        parent::before();

        if (!Cookie::get('admin', null) || !$this->request->is_ajax()) die('Access denied');
    }

    public function action_add() {
        $db = Kohana::$config->load('database.default.connection.database');
        $owner = $this->request->post('owner');
        $owner_id = $this->request->post('owner_id');
        $owned = strtolower($this->request->post('owned'));
        $owned_id = $this->request->post('owned_id');
        $new_table_name = $owner.'_has_many_'.$owned;

        $sql = '
            CREATE TABLE IF NOT EXISTS
                `'.$db.'`.`'.$new_table_name.'` (
                    `id` INT(11) NOT NULL AUTO_INCREMENT,
                    `'.$owner.'_id` INT(11) NOT NULL,
                    `'.$owned.'_id` INT(11) NOT NULL,
                    PRIMARY KEY(`id`)
                )
            ENGINE = InnoDB
        ';

        try {
            DB::query(NULL, $sql)->execute();
            DB::insert($new_table_name, array($owner.'_id', $owned.'_id'))->values(array($owner_id, $owned_id))->execute();
            die(json_encode(array('status' => 'ok')));
        } catch (Exception $e) {
            die(json_encode(array('status' => 'error', 'data' => $e)));
        }
    }

    public function action_show() {
        $owner = $this->request->post('owner');
        $owner_id = $this->request->post('owner_id');
        $owned = strtolower($this->request->post('owned'));
        $new_table_name = $owner.'_has_many_'.$owned;
        $waymark = $this->request->post('waymark');

        try {
            $result = DB::select($owner.'_id', $owned.'_id', $owned.'.id', $owned.'.'.$waymark)->from($new_table_name)->where($new_table_name.'.'.$owner.'_id', '=', $owner_id)->join($owned, 'LEFT')->on($new_table_name.'.'.$owned.'_id', '=', $owned.'.id')->as_object()->execute();
            $d = View::factory('aajax/ahasmanylist', array(
                'result' => $result,
                'waymark' => $waymark,
                'in_for_the_del' => array(
                    'owner' => $owner,
                    'owned' => $owned,
                    'owner_id' => $owner_id
                )
            ));

            echo $d;
        } catch (Exception $e) {
            die(json_encode(array('status' => 'error', 'data' => $e)));
        }
    }

    public function action_del() {
        $owner = $this->request->post('owner');
        $owner_id = $this->request->post('owner_id');
        $owned = strtolower($this->request->post('owned'));
        $owned_id = $this->request->post('owned_id');
        $new_table_name = $owner.'_has_many_'.$owned;

        try {
            echo DB::delete($new_table_name)->where($owner.'_id', '=', $owner_id)->and_where($owned.'_id', '=', $owned_id)->execute();
            die(json_encode(array('status' => 'ok', 'data' => NULL)));
        } catch (Exception $e) {
            die(json_encode(array('status' => 'error', 'data' => $e)));
        }
    }

}